home *** CD-ROM | disk | FTP | other *** search
- /* ASCII Transfer External for Hermes © 1990 By John Raymonds.
-
- Written in THINK C 4.0.1 as an example external for Hermes protocol
- developers. Use of any code shown below in any program other than
- an external to be used with Hermes requires written permission from
- the author:
-
- John Raymonds
- 76174,205 (Compuserve)
- D3885 (AppleLink)
-
- Comments and suggestions are welcome.
-
- */
-
- #include "Protocol.h"
- #include "ASCII.h"
-
- extern ProtoRecPtr PRP;
- extern ProtoGloPtr PGP;
-
- RecvASCII()
- {
- OsErr err;
- register ProtoRecPtr prp;
- register ProtoGloPtr pgp;
- int errCount, conErr, Done, byteCount, c;
- pgp = PGP;
- prp = PRP;
- err = noErr;
- conErr = 0;
- if (prp->F.B.transMode != TERMINALMODE) {
- SendString(0L);
- SendString("\pASCII 1.0d6 - Type Control-K to abort,");
- SendString("\por Control-D to Finish");
- SendString(0L);
- FlushWrite();
- }
- Pstrcpy(pgp->fName, *prp->fList[prp->filesDone].fName);
- prp->startTime = TickCount();
- prp->bytesDone = 0;
- /* since we are doing an ASCII transfer we do not know how long
- the file is.
- */
- prp->bytesTotal = -1L;
- /* open file */
- pgp->FP.ioCompletion = 0L;
- pgp->FP.ioNamePtr = pgp->fName;
- pgp->FP.ioVRefNum = prp->fList[prp->filesDone].vRefNum;
- pgp->FP.ioFVersNum = 0;
- err = PBCreate(&pgp->FP, FALSE);
- pgp->FP.ioFDirIndex = 0;
- PBGetFInfo(&pgp->FP, FALSE);
- if (!err) {
- pgp->FP.ioFlFndrInfo.fdType = 'TEXT';
- pgp->FP.ioFlFndrInfo.fdCreator = pgp->thePrefs.fType;
- PBSetFInfo(&pgp->FP, FALSE);
- /* open the data fork */
- pgp->DIOP.ioCompletion = 0L;
- pgp->DIOP.ioNamePtr = pgp->fName;
- pgp->DIOP.ioVRefNum = prp->fList[prp->filesDone].vRefNum;
- pgp->DIOP.ioVersNum = 0;
- pgp->DIOP.ioPermssn = fsRdWrPerm;
- pgp->DIOP.ioMisc = 0L;
- err = PBOpen(&pgp->DIOP, FALSE);
- }
- byteCount = 0;
- Done = FALSE;
- while (!Done) {
- errCount = pgp->errCount;
- c = WaitForChar(15);
- switch(c) {
- case TIMEOUT:
- tellerror(TIMEOUT);
- break;
- case ABORT:
- Done = TRUE;
- tellerror(CANCELED);
- /* since the transfer was aborted by the sender there is
- no reason to set errFatal... (he wanted to stop)
- */
- break;
- case FINISHED:
- pgp->DIOP.ioBuffer = (Ptr) &pgp->theData[0];
- pgp->DIOP.ioReqCount = byteCount;
- pgp->DIOP.ioPosMode = fsAtMark;
- err = PBWrite(&pgp->DIOP, FALSE);
- byteCount = 0;
- /* close file */
- PBClose(&pgp->DIOP, FALSE);
- PBFlushVol(&pgp->DIOP, FALSE);
- pgp->DIOP.ioRefNum = 0;
- /* tell Hermes that the transfer was successful */
- prp->F.B.newFile = TRUE;
- Return(0, &pgp->SSR);
- ++prp->filesDone;
- /* stop the loop */
- Done = TRUE;
- break;
- default:
- pgp->theData[byteCount++] = c;
- if (byteCount == 1024) {
- pgp->DIOP.ioBuffer = (Ptr) &pgp->theData[0];
- pgp->DIOP.ioReqCount = byteCount;
- pgp->DIOP.ioPosMode = fsAtMark;
- err = PBWrite(&pgp->DIOP, FALSE);
- prp->bytesDone += pgp->DIOP.ioActCount;
- byteCount = 0;
- }
- break;
- }
- if (errCount != pgp->errCount) {
- conErr++;
- if (conErr >= MAXERR) {
- pgp->errFatal = TOOMANYERR;
- }
- }
- else {
- conErr = 0;
- }
- if (err || prp->F.B.carrierLost) {
- Done = TRUE;
- }
- if (prp->F.B.stopTrans || (conErr >= MAXERR)) {
- Done = TRUE;
- }
- }
- /* see if the file is still open */
- if (pgp->DIOP.ioRefNum) {
- /* close file */
- PBClose(&pgp->DIOP, FALSE);
- PBFlushVol(&pgp->DIOP, FALSE);
- pgp->DIOP.ioRefNum = 0;
- /* something was wrong if it was still open */
- PBDelete(&pgp->FP, FALSE);
- }
- /* flush any characters still coming in */
- FlushModemInput();
- if (prp->F.B.transMode != TERMINALMODE) {
- SendString(0L);
- SendString(0L);
- SendString("\pASCII Receive Finished...");
- }
- FlushWrite();
- /* we are reserving err for Mac OS errors only...
-
- errFatal is keeping track of any protocol erorrs that may
- make the transfer fail.
- */
- if (!err) {
- if (pgp->errFatal) {
- SetError(pgp->errFatal);
- }
- }
- else {
- SetError(err);
- }
- /* MAKE SURE WE RETURN WITH A NON-ZERO ERROR CODE!!! */
- if (!err) {
- err = 128;
- }
- return(err);
- }
-